package jwf.util;

public class Hex {

    public static String format(byte[] data) {
	StringBuffer result = new StringBuffer();
	int n = 0;
	for (byte b : data) {
	    if (n % 16 == 0) {
		result.append(String.format("%04X: ", n));
	    }
	    result.append(String.format("%02X ", b));
	    n++;
	    if (n % 16 == 0) {
		result.append("\n");
	    }
	}
	return result.toString();
    }

    public static String format(byte[] data, int len) {
	StringBuffer result = new StringBuffer();
	int n = 0;
	for (int i = 0; i < len; ++i) {
	    byte b = data[i];
	    if (n % 16 == 0) {
		result.append(String.format("%04X: ", n));
	    }
	    result.append(String.format("%02X ", b));
	    n++;
	    if (n % 16 == 0) {
		result.append("\n");
	    }
	}
	return result.toString();
    }

}
